/**
 * 生成时间刻度数组
 * 
 * @description 根据开始时间、总时长和刻度间隔，生成时间轴上的刻度点数组
 * @param ganttData - 甘特图数据对象
 * @param startDateTime - 开始时间
 * @param totalHours - 总时长
 * @param hourWidth - 每小时宽度
 * @param tickIntervalMins - 刻度间隔（分钟）
 * @returns 时间刻度数组
 */

import { computed, ComputedRef } from 'vue'
import { GanttData, TimeTick } from './types'

export function useTimeScaleTicks(
  ganttData: GanttData,
  startDateTime: ComputedRef<Date>,
  totalHours: ComputedRef<number>,
  hourWidth: ComputedRef<number>,
  tickIntervalMins: ComputedRef<number>
): ComputedRef<TimeTick[]> {
  return computed(() => {
    const ticks: TimeTick[] = []
    const startTime = startDateTime.value.getTime()

    // 固定生成间隔为 15分钟，保证小刻度始终存在
    const generationInterval = 15
    const intervalMs = generationInterval * 60 * 1000
    // 标签显示的间隔 (由底部选项决定)
    const labelInterval = tickIntervalMins.value

    const pxPerMin = hourWidth.value / 60

    // 对齐起始时间 (向下取整到最近的 15分钟)
    // 这样保证生成的刻度时间总是 00, 15, 30, 45 分
    let loopTime = Math.floor(startTime / intervalMs) * intervalMs
    const endTime = startTime + totalHours.value * 60 * 60 * 1000

    while (loopTime <= endTime) {
      const diffMs = loopTime - startTime
      const left = (diffMs / (1000 * 60)) * pxPerMin

      // 只处理在可视范围内的刻度 (允许一点误差)
      if (left >= -1) {
        const currentTickTime = new Date(loopTime)
        const currentHour = currentTickTime.getHours()
        const currentMin = currentTickTime.getMinutes()

        // Major Tick 判断 (是否显示 Label)
        let isMajor = false

        if (labelInterval < 60) {
          // 如果选的是 15m/30m，我们只在整点显示 Label
          isMajor = (currentMin === 0)
        } else {
          // 如果选的是 1h/2h/4h...，我们只在符合间隔的整点显示 Label
          // 比如 2h: 0, 2, 4... (currentMin必须为0)
          const stepHours = labelInterval / 60
          isMajor = (currentMin === 0) && (currentHour % stepHours === 0)
        }

        // Label
        let label = ''
        let dayLabel = '' // D日标签

        if (isMajor) {
          label = String(currentHour).padStart(2, '0')
          
          // 如果是 00 点，计算 D+X
          if (label === '00') {
            const dDayStr = ganttData.simulation.currentDate
            if (dDayStr) {
              const dDay = new Date(dDayStr.replace(/-/g, '/'))
              dDay.setHours(0, 0, 0, 0)
              
              const tickDay = new Date(currentTickTime)
              tickDay.setHours(0, 0, 0, 0)
              
              const diffTime = tickDay.getTime() - dDay.getTime()
              const diffDays = Math.round(diffTime / (24 * 3600 * 1000))
              
              if (diffDays === 0) {
                dayLabel = 'D日'
              } else if (diffDays > 0) {
                dayLabel = `D+${diffDays}`
              } else {
                dayLabel = `D${diffDays}`
              }
            } else {
              dayLabel = 'D日'
            }
          }
        }

        ticks.push({
          label,
          dayLabel,
          left,
          isMajor
        })
      }

      loopTime += intervalMs
    }
    
    return ticks
  })
}
